/**
 * 
 */
package com.hs.app.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.hs.app.dao.IProduceDao;
import com.hs.core.dao.impl.BaseDao;
import com.hs.core.utils.PageParam;
import com.hs.core.utils.PageQuery;
import com.hs.core.utils.PageUtil;

/**
 * @author shl
 *
 * Dec 9, 2010
 */
@Repository 
public class ProduceDao extends BaseDao implements IProduceDao{
	public PageUtil getPageList(PageParam pageParam){
		PageQuery pq = new PageQuery(pageParam);
		Query query = null;
		Query countquery = null;
		query = this.getSession().createQuery(pageParam.getSearchSql());
		Map map =pageParam.getConditionMap(); 
		String sql = pageParam.getSearchSql().toLowerCase();
		int index = sql.indexOf("from");
		String sqlall = pageParam.getSearchSql();
		countquery = this.getSession().createQuery("select count(*) " + sqlall.substring(index)); 
 		PageUtil pu = pq.getPageUtil(query,countquery); 
		return pu;
	}

	/**
	 * 根据对应属性删除对应数据
	 * @param clazz
	 * @param params
	 * @param values
	 */
	public void deleteBean(Class<?> clazz,String[] params,String[] values){
		if(null != clazz){
			if(null != params && null != values && params.length == values.length){
				StringBuilder hsql = new StringBuilder();
				hsql.append("delete " + clazz.getName() + " where 1 = 1 ");
				for(int index = 0;index < params.length;index++){
					hsql.append(" and " + params[index] + " = '" + values[index] + "'");
				}
				executeUpdate(hsql.toString());
			}
		}
	}
	
	/**
	 * 根据多个条件获取数据集合
	 * @param clazz
	 * @param parames
	 * @param values
	 */
	@SuppressWarnings("unchecked")
	public List<Object> findBeanList(Class<?> clazz,String[] params,String[] values){
		List<Object> list = null;
		if(null != clazz){
			if(null != params && null != values && params.length == values.length){
				StringBuilder hsql = new StringBuilder();
				hsql.append("from " + clazz.getName() +" where 1 = 1");
				for(int index = 0;index < params.length;index++){
					hsql.append(" and " + params[index] + " = '" + values[index] + "'");
				}
				hsql.append(" order by id");
				list = findList(hsql.toString());
			}
		}
		return list;
	}
}
